using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ListaFacil
{
    public partial class frmCadastroSecaoDisque : Form
    {
        private DataTable dtSecaoDisque;
        private BindingSource bsSecaoDisque = new BindingSource();
        private bool editMode = false;

        public frmCadastroSecaoDisque()
        {
            InitializeComponent();
        }
        private void HabilitaBotoes(bool flagHabilita)
        {
            bindingNavigatorAddNewItem.Enabled = flagHabilita;
            bindingNavigatorDeleteItem.Enabled = flagHabilita;
            bindingNavigatorMoveFirstItem.Enabled = flagHabilita;
            bindingNavigatorMoveLastItem.Enabled = flagHabilita;
            bindingNavigatorMoveNextItem.Enabled = flagHabilita;
            bindingNavigatorMovePreviousItem.Enabled = flagHabilita;
            bindingNavigatorPositionItem.Enabled = flagHabilita;
            editToolStripButton.Enabled = flagHabilita;
            saveToolStripButton.Enabled = !flagHabilita;
            cancelToolStripButton.Enabled = !flagHabilita;
            txtNome.Enabled = !flagHabilita;
            
            dataGridView1.Enabled = flagHabilita;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                lblId.Text= dataGridView1.CurrentRow.Cells["id_secao_disque"].Value.ToString();
                txtNome.Text = dataGridView1.CurrentRow.Cells["nome"].Value.ToString();
                this.Navegacao(this, e);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
        }

        private void EditToolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
            this.editMode = true;
        }

        private void cancelToolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(true);
        }

        private void dtTesteRowDeleting(object sender, EventArgs e)
        {

            int numeroregistro = 0;
            int linha = 0;
            string retorno = "";

            linha = Convert.ToInt32(bindingNavigator1.PositionItem.Text) - 1;
            numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells[0].Value);

            retorno = Program.SLF.removerSecaoDisque(numeroregistro);

            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            PreencheGrid();
           

        }
        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            int numeroregistro = 0;
            int linha = 0;
            string retorno = "";

            linha = Convert.ToInt32(bindingNavigator1.PositionItem.Text) - 1;
            numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells[0].Value);

            retorno = Program.SLF.removerSecaoDisque(numeroregistro);

            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            PreencheGrid();
            this.HabilitaBotoes(true);
        
        }
        private void PreencheGrid()
        {
            dtSecaoDisque = Program.SLF.consultarSecoesDisqueCadastradas();
            bsSecaoDisque.DataSource = dtSecaoDisque;
            bindingNavigator1.BindingSource=bsSecaoDisque;
            dataGridView1.DataSource=bsSecaoDisque;
        }


        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string retorno = "";
            int numeroregistro = 0;
            int linha = 0;

            if (this.editMode == false)
            {
                retorno = Program.SLF.inserirSecaoDisque(txtNome.Text);

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) - 1;
                numeroregistro = Convert.ToInt32(dataGridView1.Rows[linha].Cells[0].Value);
                retorno = Program.SLF.updateSecaoDisque(txtNome.Text, Int32.Parse(lblId.Text));

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            PreencheGrid();
            this.HabilitaBotoes(true);
        }

        private void txtNome_TextChanged(object sender, EventArgs e)
        {
            if (this.editMode)
                this.HabilitaBotoes(false);
        }

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Navegacao(object sender, EventArgs e)
        {
            this.editMode = false;
        }

        private void Edicao(object sender, EventArgs e)
        {
            this.editMode = true;
        }

        private void frmCadastroSecaoDisque_Load(object sender, EventArgs e)
        {
            dtSecaoDisque = Program.SLF.consultarSecoesDisqueCadastradas();
            bsSecaoDisque.DataSource = dtSecaoDisque;
            bindingNavigator1.BindingSource = bsSecaoDisque;
            dataGridView1.DataSource = bsSecaoDisque;
            this.HabilitaBotoes(true);

        }
    }
}